package com.example.planetandzombiedemo.entity;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;

import com.example.planetandzombiedemo.global.Config;
import com.example.planetandzombiedemo.model.BaseModel;
import com.example.planetandzombiedemo.model.Touchable;
import com.example.planetandzombiedemo.view.GameView;

public class EmplacePea extends BaseModel implements Touchable {
    private Rect touchArea;
    public EmplacePea(int locationX,int locationY)
    {
        this.locationX = locationX;
        this.locationY = locationY;
        this.isAlife = true;
        touchArea = new Rect(0,0, Config.deviceWidth,Config.deviceHeight);
    }
    @Override
    public void drawSelf(Canvas canvas, Paint paint) {
        if (isAlife) {
            canvas.drawBitmap(Config.peaFrames[0],locationX,locationY,paint);
        }
    }

    @Override
    public int getModelWidth() {
        return Config.peaFrames[0].getWidth();
    }

    @Override
    public boolean onTouch(MotionEvent event) {
        int x=(int)event.getRawX();
        int y=(int)event.getRawY();
        if(touchArea.contains(x,y))
        {//触摸三种状态：按下（down）--》移动（move）--》抬起（up）
            switch (event.getAction())
            {
                case MotionEvent.ACTION_DOWN:
                    break;
                case MotionEvent.ACTION_MOVE:
                    locationX=x-Config.peaFrames[0].getWidth()/2;
                    locationY=y-Config.peaFrames[0].getHeight()/2;
                    break;
                case MotionEvent.ACTION_UP:
                    isAlife=false;//放置的动作结束，不在显示放置的元素
                    GameView.getInstance().apply4Plant(locationX,locationY,this);
                    break;
            }
        }
        return false;
    }
}
